import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    //牛客top101——二叉树
    /**
     * 二叉树的前序遍历
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public int[] preorderTraversal (TreeNode root) {
        List<Integer>list = new ArrayList<>();
        preorder(list,root);
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
    public void  preorder(List<Integer> list, TreeNode root){
        if (root == null){
            return;
        }
        list.add(root.val);
        preorder(list,root.left);
        preorder(list,root.right);
    }

    /**
     * 二叉树的中序遍历
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public int[] inorderTraversal (TreeNode root) {
        List<Integer>list = new ArrayList<>();
        Inorder(list,root);
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    private void Inorder(List<Integer> list, TreeNode root) {
        if (root == null){
            return;
        }

        Inorder(list,root.left);
        list.add(root.val);
        Inorder(list,root.right);
    }

    /**
     * 二叉树从后序遍历
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public int[] postorderTraversal (TreeNode root) {
        List<Integer>list = new ArrayList<>();
        postorder(list,root);
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    private void postorder(List<Integer> list, TreeNode root) {
        if (root == null){
            return;
        }
        postorder(list,root.left);
        postorder(list,root.right);
        list.add(root.val);
    }

    /**
     * 求二叉树的层序遍历
     * @param root TreeNode类
     * @return int整型ArrayList<ArrayList<>>
     */
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {

    }

}

